
$(document).ready(function() {
	// 设置输入框的提示
	$('a.hide').click(function() {
		$(".cart").toggle("slow");
	});
	
	$(".increase_num").click(function() {
		update($(this),"increase");
	});
	$(".decrease_num").click(function() {
		var count = $(this).siblings(".item-buy-quantity-input").val();
		if(count==1) return;
		update($(this),"decrease");
	});
	
	$(".item-buy-quantity-input").keyup(function() {
		update($(this),$(this).val());
	});
	
	$(".item-buy-delete").click(function() {
		deleteItem($(this), "deleteOne");
	});
	
	$(".clean_cart").click(function() {
		if(confirm('您确定要清空购物车吗？此操作不能恢复哦~')){
			deleteAll();
		}
	});
	
	//购买
	$("#sure").click(function() {
		var payWay = $("input:checked").val().trim();
		//if(payWay=="alipay"){
		//	window.location.href=$(this).attr("rel");
		//}else{
			var userName =$("#userName").html().trim();//用户名
			var toAddr = $("input#AddressTextBox").val().trim();
			var toUser =userName +" ["+$("input#PhoneNumTextBox").val().trim()+ "]";
			var toTime = $("input#MoreTextBox").val().trim();
			var isPaid = "true";
			//alert(userName+"--"+toAddr+"---"+toUser+"--"+toTime);
			saveOrder(userName, toAddr, toUser, toTime, isPaid);
		//}
	});
});


//实时修正数量价格函数
function update(tag,action){
	var userName =$("#userName").html().trim();//用户名
	var number = tag.siblings(".item-buy-quantity-input");//商品数量标签
	var goodsId = tag.parents("tr.cart_item").attr("id");//商品Id
	var goodsPrice = tag.parents("tr.cart_item").children("td.price_box").children("span").html();//商品价格
	var countPrice = tag.parents("tr.cart_item").children("td.count_price_box").children("span");//商品小计标签
	$.ajax({
		type : "POST",
		url : "servlet/TCartServlet",
		data : "action=update&goodsId="+goodsId+"&userName=" +userName+"&update="+action,
		success : function(result) {
			var result= result.trim();
			if(result=="failed"){
				alert("亲~~~ ， 修改数量失败了~~"+result*goodsPrice);
			}else{
				number.attr("value",result);//修改数量
				countPrice.html((result*goodsPrice).toFixed(2));//修改小计
				//alert(""+result+"====="+goodsPrice+"==="+totalPrice.html());
				calculate();//修改总计
			}
		}
	});
}

//删除订单项
function deleteItem(tag,action){
	var userName =$("#userName").html().trim();//用户名
	//var number = tag.siblings(".item-buy-quantity-input");//商品数量标签
	var goodsId = tag.parents("tr.cart_item").attr("id");//商品Id
	//var goodsPrice = tag.parents("tr.cart_item").children("td.price_box").children("span").html();//商品价格
	//var countPrice = tag.parents("tr.cart_item").children("td.count_price_box").children("span");//商品小计标签
	
	$.ajax({
		type : "POST",
		url : "servlet/TCartServlet",
		data : "action=delete&goodsId="+goodsId+"&userName=" +userName,
		success : function(result) {
			var result= result.trim();
			if(result=="success"){
				tag.parents("tr.cart_item").toggle("slow");
				tag.parents("tr.cart_item").remove();
				calculate();//修改总计
			}else {
				alert("亲~~~ ， 删除失败了~~");
			}
		}
	});
}

//清空购物车
function deleteAll(){
	var userName =$("#userName").html().trim();//用户名
	//var number = tag.siblings(".item-buy-quantity-input");//商品数量标签
	//var goodsId = tag.parents("tr.cart_item").attr("id");//商品Id
	//var goodsPrice = tag.parents("tr.cart_item").children("td.price_box").children("span").html();//商品价格
	//var countPrice = tag.parents("tr.cart_item").children("td.count_price_box").children("span");//商品小计标签
	
	$.ajax({
		type : "POST",
		url : "servlet/TCartServlet",
		data : "action=deleteAll&goodsId=0&userName=" +userName,
		success : function(result) {
			var result= result.trim();
			if(result=="success"){
				$(".cart_products").toggle("slow");
				$(".cart_products").show("slow");
				$(".cart_products").html("<div class='cart_empty'><span>您的购物车中还没有商品，赶快去挑选吧！</span>"
					+" <a href='index.jsp'>点击返回商城首页</a></div>");
			}else {
				alert("亲~~~ ， 清楚购物车失败了~~");
			}
		}
	});
}

function calculate(){
	var totalPrice = $(".totalPrice span");//总价格标签
	var total = 0;
	$.each($("td.count_price_box"),function(index,value){
		var price  = Number($(this).children("span").html());
		total+=price;
		//alert(price);
	});
	totalPrice.html(total.toFixed(2));
}	

//保存订单信息
function saveOrder(userName,toAddr,toUser,toTime,isPaid){
	var userName =$("#userName").html().trim();//用户名
	$.ajax({
		type : "POST",
		url : "servlet/OrderServlet",
		data : "action=saveOrder_tg&userName="+userName+"&toAddr="+toAddr+"&toUser="+toUser+"&toTime="+toTime+"&isPaid="+isPaid,
		success : function(result) {
			var result= result.trim();
			var arr = result.split(",");
			if(arr[0]=="failed"){
				alert("亲~~~你的订单提交失败了哦");
			}else {
				window.location.href="orderSuccess.jsp?orderNo="+arr[0]+"&randomNum="+arr[1];
			}
		}
	});
}